let api = require('../../../api/api.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    oaderTab: [
      {
        name: '全部',
        active: true
      },
      {
        name: '待支付',
        active: false,
        status:1
      },
      {
        name: '待发货',
        active: false,
        status:2
      },
      {
        name: '配送中',
        active: false,
        status:5
      },
      {
        name: '已收货',
        active: false,
        status:3

      }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      options:options
    })
  },
  okData(e){
    let data = {
      order_sn: e.currentTarget.id
    }
    api.confirmarrival(data,(res)=>{
      
      let data = {
        status: 5
      }
      this.orderLists(data)
    })
  },
  orderLists(data){
    api.orderList(data,(res)=>{
      console.log(res)
      this.setData({
        orderData: res.data.result
      })
      wx.hideLoading();
    })
  },
  tabToggle: function (e){
    let index = e.currentTarget.dataset.index
    this.data.oaderTab.forEach((item, index) => { // 设置切换样式
      let key = `oaderTab[${index}].active`;
      this.setData({
        [key]: false
      })
    })
    this.setData({
      [`oaderTab[${index}].active`]: true,
    })
    let data = {
      status: e.currentTarget.id
    }
    this.orderLists(data)
    wx.showLoading({
      title: '加载中',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  content(e){//订单号跳转
    let id = e.currentTarget.id
    console.log(id)
    wx.navigateTo({
      url: `/pages/my/orderContent/orderContent?id=${id}`,
    })
  },
  onReady: function () {
    
  },
  url(){
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  urlIndex(){
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  logistics(e){
    let id = e.currentTarget.id
    wx.navigateTo({
      url: `/pages/buy/site/site?id=${id}`,
    })
  },
  goPay(e){
      console.log(e)
      let id = e.currentTarget.id
      let data = {
        order_sn:id
      }
      let that = this
    api.payGo(data,(res)=>{
      wx.requestPayment({
        'timeStamp': res.data.result.timeStamp,
        'nonceStr': res.data.result.nonceStr,
        'package': res.data.result.package,
        'signType': 'MD5',
        'paySign': res.data.result.paySign,
        'success': function (res) {
          console.log(res, '支付成功')
          that.onLoad();
        },
        'fail': function (res) {
          wx.showToast({
            title: '支付失败',
            icon: "none"
          })
        },
        'complete': function (res) { }
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var options = this.data.options
    let data = {}
    //确定是从上一级页面的那个index跳转的
    if (options.id == 0) {
      data.status = ''
    } else {
      data.status = options.id
    }
    var result = this.data.oaderTab
    result.forEach(function (element) {
      if (element.status == options.id) {
        element.active = true
      } else {
        element.active = false
      }
    })
    this.orderLists(data)
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      oaderTab: result
    })
  },
  phone:function(){
    wx.makePhoneCall({
      phoneNumber: '18511175656',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },


})